/*
 * $Id: SkvEnvPath.cc,v 1.3 2006-04-24 08:42:10 bacon Exp $
 */

#include <xpp/utl/SkvEnvPath.h>
#include <xp/bas/assert.h>

namespace xpp
{
	namespace utl
	{

		int SkvEnvPath::read (SkvEnv* env) 
		{
			int n = SkvListPath::read ((SkvList*)env);
		
			if (n != -1) {
				// validate the entries found in the file.
				SkvList::Node* np;
				SkvSctn::Node* np2;	
				for (np = ((SkvList*)env)->head(); np; np = np->forward()) {
					SkvSctn& s = np->value;
					for (np2 = s.head(); np2; np2 = np2->forward()) {
						SkvPair& p = np2->value;
						xp_char_t* name, * dval;
						if (env->getItemNameAndDval (
							s.name(), p.key.buffer(), 
							&name, &dval) == -1) continue;
			
						const xp_char_t* val = 
							env->setItemValue (name, p.value.buffer());
						if (val == XP_NULL) val = env->setItemValue (name, dval);
						xp_assert (val != XP_NULL);
					}
				}
			}
		
			// reset values for missing entries...
			SkvEnv::ItemList::Node* np3;
			for (np3 = env->itemList().head(); np3; np3 = np3->forward()) {
				SkvEnv::Item& item = np3->value;
				if (env->itemValueOf (item.name) == XP_NULL) 
					env->setItemValue (item.name, item.dval);
			}	
		
			return n;
		}

		int SkvEnvPath::write (SkvEnv* env)
		{
			return SkvListPath::write ((SkvList*)env);
		}	

		int SkvEnvPath::write (SkvEnv* env, const xp_char_t* file)
		{
			return SkvListPath::write ((SkvList*)env, file);
		}	
		
		int SkvEnvPath::append (SkvEnv* env) 
		{
			return SkvListPath::write ((SkvList*)env, 1);
		}	

		int SkvEnvPath::append (SkvEnv* env, const xp_char_t* file) 
		{
			return SkvListPath::write ((SkvList*)env, file, 1);
		}	
		
	}
}
